/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.file.fdo;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class TableID {

    private String tableIDKey = "TableIDKey";
    private int tableID;

    private String datadir = null;
    

    public TableID(int tableID){
    	this.tableID = tableID;
		InputStream fIn = this.getClass().getResourceAsStream("/blobdb/blobdb.properties");
		BufferedReader bfIn = new BufferedReader(new InputStreamReader(fIn));
		Properties p = new Properties();
		try{
			p.load(bfIn);
			this.datadir = p.getProperty("database.dir", null);
			bfIn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
   		PMF.hTableID.put(tableID, this);
    }

    private synchronized int getCurrentId(){
    	int iRet = -1;
    	try{
    		File f = new File(this.datadir + "/" + "TableID.dat");
    		if(f.exists()){
    			FileInputStream fIn = new FileInputStream(f);
    			DataInputStream dfIn = new DataInputStream(fIn);
    			iRet = dfIn.readInt();
    			fIn.close();
    		}else{
    			iRet = 1;
    		}
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
    	return iRet;
    }
    
    public String getTableIDKey(){
    	return this.tableIDKey;
    }
	
	public int getTableID() {
		return this.getCurrentId();
	}

	public void setTableID(int tableID) {
    	try{
    		File f = new File(this.datadir + "/" + "TableID.dat");
    		FileOutputStream fOut = new FileOutputStream(f);
    		DataOutputStream dfOut = new DataOutputStream(fOut);
    		dfOut.writeInt(tableID);
    		dfOut.flush();
    		dfOut.close();
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
	}
    
	public void save(){
	}
    
}


